﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lite;
using System.Data.SqlClient;
using Csla.Data;
using Csla;
using OpenExpressApp;
using System.Collections;
using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp.Types;
using OpenExpressApp.Command;

namespace OpenExpressApp.Module.Library
{
    public partial class BoInfoOperationList : GBusinessListBase<BoInfoOperationList, BoInfoOperation>
    {
        public BoInfoOperationList()
        {
            AllowNew = true;
        }

        /// <summary>
        /// 根据类型来获取Command列表
        /// </summary>
        /// <param name="boType"></param>
        /// <returns></returns>
        public static BoInfoOperationList GetList(BusinessObjectInfo boInfo)
        {
            return DataPortal.Fetch<BoInfoOperationList>(boInfo);
        }

        [RunLocal]
        private void DataPortal_Fetch(BusinessObjectInfo boInfo)
        {
             RaiseListChangedEvents = false;
             OpenExpressApp.Command.CommandBase[] commands = null;
             if (boInfo.IsDefaultObject)
            {
                ModuleType moduleType = boInfo.DefaultObjectAttribute.ModuleType;
                commands = ApplicationModel.Commands.Where(
                     c => ((c.TargetObjectType == boInfo.BOType) || (c.TargetObjectType == null)) &&
                        ((c.ModuleType == ModuleType.Unspecified) || (c.ModuleType == moduleType)) &&
                        ((c.ToolbarType == ToolbarType.Any) || (c.ToolbarType == ToolbarType.Main))
                     ).ToArray();
            }
            else
            {
                commands = ApplicationModel.Commands.Where(c =>
                     (c.TargetObjectType == boInfo.BOType || c.TargetObjectType == null) &&
                     (c.ToolbarType == ToolbarType.Any || c.ToolbarType == ToolbarType.Child)
                     ).ToArray();
            }
             foreach (var command in commands)
            {
                //过滤不可见的command
                bool notVisible = ApplicationModel.IsNotVisibleCommand(command.Id, boInfo);
                if (notVisible) continue;
                this.Add(new BoInfoOperation(command, boInfo));
            }

            RaiseListChangedEvents = true;
        }

        /// <summary>
        /// 根据整个聚合对象集的Command列表
        /// </summary>
        /// <param name="boType"></param>
        /// <returns></returns>
        public static BoInfoOperationList GetAggregationteList(BusinessObjectInfo boInfo)
        {
            BoInfoOperationList result = new BoInfoOperationList();
            foreach (var item in boInfo.ObjectInfoListInPermission)
            {
                BoInfoOperationList list = DataPortal.Fetch<BoInfoOperationList>(item);
                result.AddRange(list);
            }
            return result;
        }
    }

    [Serializable]
    [BusinessObject("374E5DB3-1A0C-42cc-8604-45B899BA4DCB", PropertyGroup = "ObjectName"), Label("对象功能")]
    public partial class BoInfoOperation : GBusinessBase<BoInfoOperation>
    {
        public BoInfoOperation(OpenExpressApp.Command.CommandBase cmd, BusinessObjectInfo boInfo)
        {
            Operation = new Operation(cmd);
            this.BusinessObjectInfo = boInfo;
        }

        public BusinessObjectInfo BusinessObjectInfo { get; set; }
        [EntityProperty, Label("对象名称")]
        public string ObjectName
        {
            get
            {
                return BusinessObjectInfo.Label;
            }
        }

        public Operation Operation { get; set; }
        [EntityProperty, Label("功能名称")]
        public string OperationName
        {
            get
            {
                return Operation.Name;
            }
        }


        public Guid BusinessObjectId
        {
            get { return BusinessObjectInfo.Id; }
        }

        public Guid OperationId
        {
            get { return Operation.Id; }
        }

    }
}